Skip to content

test: add failing regression tests for the cell origin of CIF files - #207

Draft
sbillinge wants to merge 2 commits into
mainfrom
cif-origin-tests
Draft

sbillinge wants to merge 2 commits into
mainfrom
cif-origin-tests

Conversation

@sbillinge

Copy link
Copy Markdown
Contributor

Adds tests/test_cif_origin.py, a set of regression tests for how the CIF parser
chooses the unit cell origin when it expands a structure. Relates to #86, #183,
diffpy/diffpy.pdfgui#291 and diffpy/diffpy.srfit#213.

This PR is expected to be red. It contains tests only. The fix will be
developed in a PR against this branch (cif-origin-tests), and this branch is
merged into main once the tests are green. Please do not merge it before then.

  • 574 new test cases: 408 fail on current main, 166 pass as guards.
  • The 240 existing tests are unchanged and pass.
  • No source changes. The news item says no news is needed; the fix adds its own.

closes #86 once the fix has been merged into this branch.

🤖 Generated with Claude Code

  1. Comment for the PR and your student

Background: what these tests describe

A CIF file defines its symmetry, and therefore where the cell origin sits, in one
of these ways, from most to least explicit:

  1. a list of symmetry operations (_space_group_symop_operation_xyz or
    _symmetry_equiv_pos_as_xyz),
  2. a Hall symbol (_space_group_name_Hall),
  3. a Hermann-Mauguin symbol, possibly with an origin qualifier (F d -3 m :2, or
    ICSD style F d -3 m Z / F d -3 m S),
  4. an International Tables number, which says nothing about the origin.

What the parser does today

  • Listed operations are used only if they exactly match one of the 514 settings
    tabulated in diffpy.structure.spacegroups. If they do not match and the file
    also has an IT number or a recognizable H-M symbol, the listed operations are
    silently dropped and the tabulated default is used.
  • With no listed operations, the IT number takes priority over the H-M symbol.
  • An H-M symbol with an origin qualifier is not recognized at all.
  • The Hall symbol is never used to look up the symmetry.

The result is the symptom in #86 and diffpy/diffpy.pdfgui#291: atoms generated
with the wrong operations, so sites are duplicated or sit too close together.

The cases

Situation Groups affected Today
Two fixed origins, file in origin choice 2 with operations listed 24 groups have two origins; the
table holds origin choice 1 for all of them, and origin choice 2 on standard axes only for 59 (Pmmn) 23
expand wrongly, silently
Same, no operations, IT number + :2 symbol all 24 wrong, silently (number wins)
Same, no operations, only a qualified symbol (:2, :1, Z, S) all 24 raises "unknown space
group identifier"
Same, no operations, IT number + Hall symbol all 24 wrong, silently
Two-origin group, nothing in the file says which origin all 24 origin choice 1, with no warning
Arbitrary non-tabulated origin, operations listed every group except P1 (229) wrong, silently
Floating origin along a polar direction 68 polar groups correct

The 24 two-origin groups are 48, 50, 59, 68, 70, 85, 86, 88, 125, 126, 129, 130,
133, 134, 137, 138, 141, 142, 201, 203, 222, 224, 227, 228.

Two points that are easy to get wrong:

  • A floating origin is not a bug. In the 68 polar groups the origin can slide
    along the polar direction, but that slide does not change the symmetry
    operations, so these files already expand correctly. Those tests pass and must
    keep passing.
  • The P2 example from fix: fix Structure loading with close-duplicate atom #183 is a different, larger class. There the twofold
    axis is moved to x = 1/4, perpendicular to the axis. That is an arbitrary,
    non-conventional origin. Any group except P1 can be written that way, and the
    file is valid as long as it lists its operations.

The behaviour we want

  1. If the file lists its symmetry operations, the structure is expanded with
    those operations, whatever origin they describe.

  2. Without listed operations, a Hall symbol or an origin qualifier on the H-M
    symbol (:1, :2, S, Z, with or without spaces) decides the origin, even
    when an IT number is also present.

  3. If the group has two origins and nothing in the file says which one is used,
    use origin choice 1 and emit exactly one UserWarning with this text:

    The CIF file uses space group number 227, which has two possible origins, but it does not say which
    origin it uses. Origin choice 1 was assumed. If the structure looks wrong, for example with atoms too
    close together, append ' :2' to the Hermann-Mauguin space group symbol in the CIF file and load it again.

    The text is the constant AMBIGUOUS_ORIGIN_WARNING at the top of the test file.

  4. No such warning when the origin is unambiguous.

The tests

Each test builds a small CIF in memory and compares the parsed structure with
the sites generated by the operations that the file defines.

Test Cases Today What it checks
test_origin_choice_2 96 95 fail 24 groups × four ways of defining origin choice 2 (operations,
number + :2, :2 only, number + Hall)
test_origin_choice_spelling 7 all fail spellings of the Fd-3m origin qualifier
test_ambiguous_origin_warning 72 all fail 24 groups × three ambiguous files: origin choice 1
plus the warning
test_unambiguous_origin_no_warning 5 pass the warning stays silent when the origin is clear
test_pyrochlore_origin_choice_2 2 all fail the Ho2Ti2O7 case from diffpy/diffpy.pdfgui#291:
multiplicities [16, 16, 48, 8], not [8, 8, 96, 32]
test_listed_symops_arbitrary_origin 229 all fail groups 2–230 with the origin moved to (1/8,
1/8, 1/8) and operations listed
test_listed_symops_handwritten 3 all fail the P2 regression suggested in #183, plus a B-centred
P2 cell
test_floating_origin 136 pass 68 polar groups, with and without listed operations
test_two_origin_table 24 pass self-check of the origin-shift table used by the tests

Total: 574 cases, 408 failing and 166 passing. The origin-choice-2 shifts and
Hall symbols in the test file were checked against an independent space group
library (gemmi), which is not a dependency of the tests.

The tests are the specification. Please do not change the expected values to
make them pass; if you think a test is wrong, say so in the PR and we will
discuss it.

One case is deliberately not covered because it has no single right answer
beyond the warning above: a plain symbol such as F d -3 m with origin choice 2
coordinates and no operations. A related problem that is also out of scope here:
for the seven rhombohedral groups, R -3 m by name resolves to rhombohedral
axes while number 166 resolves to hexagonal axes.

How to work on this (forking workflow)

The fix goes on a branch of your fork that starts from cif-origin-tests, and
your PR targets cif-origin-tests, not main.

# In your clone of your fork. Skip the first line if "upstream" already exists.
git remote add upstream https://github.com/diffpy/diffpy.structure.git
git fetch upstream
git checkout -b cif-origin-fix upstream/cif-origin-tests
git push -u origin cif-origin-fix

Run the tests:

pytest tests/test_cif_origin.py                       # everything in this file
pytest tests/test_cif_origin.py -k "handwritten"      # the three small P2 cases
pytest tests/test_cif_origin.py -k "Fd-3m"            # one group across the tests
pytest                                                # the whole suite must end green

…es from the origin the file defines, as failing regression tests for issue #86 covering origin choice 2, listed operations at arbitrary origins, and a warning for an ambiguous origin

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@sbillinge
sbillinge marked this pull request as draft November 29, 2026 15:14
@sbillinge

Copy link
Copy Markdown
Contributor Author

@stevengeorge128, @Akadib, I worked with Claude to create these tests for non-standard space-group origins. I don't mind who fixes this, or if you both work on it. The plus is that you will learn some crystallography. Please read the comment and see if you can understand what is going on and craft a solution. When all the tests turn green and this is merged, this will close #86 which is on our current release path.

This fixes a long-standing problem with our diffpy.structure that we never properly handled non-standard or ambiguous unit cell origins. I think I covered most of the situations.

@Tieqiong, @sbanerjee23 I am tagging you both. This will make you both v. happy.

@sbillinge

Copy link
Copy Markdown
Contributor Author

btw, I didn't c;heck Claude's tests, so we may want to have a crystallographer (@sbanerjee23, @cadenmyers13 ?) check them for correctness..... Don't let that hold up the fix work though. Let's work on that in parallel.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Close, repetitive atoms

1 participant